package cn.chenxiejia;

import java.io.*;
import java.util.Random;

/***
 * @program: maven
 * @ClassName: File9
 * @Description: 文件复制、创建、移动、重命名、删除
 * @Author: chenxiejia
 * @Date: 2022/1/19 14:52
 * @version : V1.0
 */
public class File9 {
    public static void main(String[] args) {
        File file = new File("E:/ab.txt");

        //创建文件
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
            Random rand = new Random();
            bw.write(String.format("%03d %tF%n", rand.nextInt(1, 900), System.currentTimeMillis()));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //文件读取
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            while (br.ready()) {
                System.out.println(br.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        //文件复制
        File file1 = new File("E:/abb.txt");
        try (FileOutputStream fos = new FileOutputStream(file1, true); FileInputStream fis = new FileInputStream(file)) {
            //fos.write(fis.readAllBytes());
            byte[] a = new byte[1024];
            int len = 0;
            while ((len = fis.read(a)) != -1) {
                fos.write(a, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        file1.renameTo(new File("E:/abbbb1.txt"));
        //文件删除

        if (file1.isFile()) {
            //file1.delete();
        }
    }

}
